﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WinRTXamlToolkit.Controls.Extensions;
using Caliburn.Micro;
using MusicExplorer.ViewModels;

// Pour en savoir plus sur le modèle d'élément Page vierge, consultez la page http://go.microsoft.com/fwlink/?LinkId=234238

namespace MusicExplorer.Views
{
    /// <summary>
    /// Une page vide peut être utilisée seule ou constituer une page de destination au sein d'un frame.
    /// </summary>
    public sealed partial class TracksPageView : Page
    {
        public TracksPageView()
        {
            this.InitializeComponent();
            DataContextChanged += TracksPageView_DataContextChanged;
        }

        private bool m_eventsLoaded;
        void TracksPageView_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
        {
            if (!m_eventsLoaded && DataContext != null && DataContext is TracksPageViewModel)
            {
                ((TracksPageViewModel)DataContext).HideLoadingEvent += HideLoadingEvent;
                ((TracksPageViewModel)DataContext).ShowLoadingEvent += ShowLoadingEvent;
                m_eventsLoaded = true;
            }
        }

        void HideLoadingEvent(object sender, EventArgs e)
        {
            if (progressGrid.Children.Count > 0)
                progressGrid.Children.RemoveAt(0);
        }

        void ShowLoadingEvent(object sender, EventArgs e)
        {
            if (progressGrid.Children.Count == 0)
            {
                var progress = new ProgressBar();
                progress.IsIndeterminate = true;
                progress.Height = 10;
                progressGrid.Children.Add(progress);
            }
        }
    }
}
